import * as path from 'path';
import { defineConfig } from 'vite';
import solidPlugin from 'vite-plugin-solid';
import devtools from 'solid-devtools/vite'
import { NodeModulesPolyfillPlugin } from "@esbuild-plugins/node-modules-polyfill";
export default defineConfig({
  plugins: [
    devtools({
      autoname: true,
    }),
    NodeModulesPolyfillPlugin({
      // @ts-ignore
      process: true,
      buffer: true,
    }),
      solidPlugin({ extensions: ['ts'] })
  ],
  resolve: {
    alias: {
      src: path.resolve('src/'),
    },
  },
  server: {
    port: 3000,
  },
  optimizeDeps: {
    exclude: ['csstype'],
      esbuildOptions: {
        target: "es2020"
      }
  },
  define: {
    'process.env': {}
  },
  build: {
    // target: 'es6',
    target: 'es2020',
    // target: 'esnext',
    outDir: 'lib',
    emptyOutDir: true,
    minify: false,
    sourcemap: true,

    lib: {
      formats: ['es', 'cjs'],
      entry: path.resolve('src/index.ts'),
      name: 'TON_CONNECT_UI',
      fileName: (format) => {
        switch (format) {
          case 'es':
            return 'index.mjs';
          case 'cjs':
            return 'index.cjs';
          default:
            throw new Error('Unknown format');
        }
      }
    },
    rollupOptions: {
      external: ['classnames', 'deepmerge', '@tonconnect/sdk', 'ua-parser-js'],
      output: {
        globals: {
          '@tonconnect/sdk': 'TonConnectSDK',
          'deepmerge': 'deepmerge',
          'classnames': 'classNames',
          'ua-parser-js': 'UAParser'
        },
      },
    },
  },
});
